package com.fyl.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
//<aop:aspect ref="myAdvice">
//            <!--前置通知-->
//            <aop:before method="beforeAdvice" pointcut="execution(void com.fyl.service.impl.*.*(..))"></aop:before>

    @Pointcut("execution(void com.fyl.service.impl.*.*(..))")
    public void  myPointCut(){}

    @Before("myPointCut()")
    public void beforeAdvice() {
        System.out.println("beforeAdvice 前置方法增强");
    }

    @AfterReturning("myPointCut()")
    public void afterReturningAdvice(JoinPoint joinPoint) {
        System.out.println("joinPoint.getArgs() = " + joinPoint.getArgs());
        System.out.println("joinPoint.getTarget() = " + joinPoint.getTarget());
        System.out.println("joinPoint.getStaticPart() = " + joinPoint.getStaticPart());
        System.out.println("afterReturningAdvice 后置方法增强");
    }

    @Around("myPointCut()")
    public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        pjp.proceed();//目标方法执行
        System.out.println("环绕后增强");
    }

    @AfterThrowing(value = "myPointCut()" ,throwing = "e")
    public void afterThrowingAdvice(Throwable e) {
        System.out.println("e = " + e);
        System.out.println("afterThrowingAdvice 异常抛出通知");
    }

    @After("myPointCut()")
    public void afterAdvice() {
        System.out.println("afterAdvice 后置方法增强");
    }
}
